package com.ozo.boot.common.base.exception;

import lombok.Getter;

/**
 * 异常信息
 *
 * @author qiangesoft
 * @date 2024-06-28
 */
@Getter
public enum CommonExceptionEnum implements AbstractExceptionEnum {

    /**
     * 请求成功
     */
    SUCCESS(200, "请求成功"),

    /**
     * 客户端错误
     */
    BAD_REQUEST(400, "请求参数错误"),
    HTTP_MESSAGE_NOT_READABLE(400, "请求消息不可重复读"),
    UNAUTHORIZED(401, "账号未登录或已过期"),
    FORBIDDEN(403, "无操作权限"),
    NOT_FOUND(404, "未找到资源"),
    METHOD_NOT_ALLOWED(405, "请求方法不正确"),
    REQUEST_TIMEOUT(408, "客户端请求超时"),
    REQUEST_TOO_LARGE(413, "请求数据包过大"),
    UNSUPPORTED_MEDIA_TYPE(415, "请求的格式不支持"),
    TOO_MANY_REQUESTS(429, "请求过于频繁，请稍后再试"),

    /**
     * 服务端错误
     */
    INTERNAL_SERVER_ERROR(500, "服务器内部错误"),
    NOT_IMPLEMENTED(501, "服务器不支持该请求"),
    BAD_GATEWAY(502, "网关代理错误"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关或代理上游服务器超时"),
    NETWORK_AUTHENTICATION_REQUIRED(505, "服务器需要网络级别的身份验证"),
    ;


    private Integer code;

    private String message;

    CommonExceptionEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static AbstractExceptionEnum getErrorCode(int code) {
        CommonExceptionEnum[] commonExceptionEnums = values();
        for (CommonExceptionEnum commonExceptionEnum : commonExceptionEnums) {
            if (commonExceptionEnum.getCode() == code) {
                return commonExceptionEnum;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }

}
